
package com.tools.cleanmaster.controller;

import android.content.ContentResolver;
import android.content.Context;

import com.tools.cleanmaster.model.TrashType;
import com.tools.cleanmaster.model.item.ImageTrashBucket;
import com.tools.cleanmaster.utils.FeatureConfig;
import com.tools.cleanmaster.utils.LogHelper;

import java.util.List;

public class ImageScanner extends TrashScanner {
    private String TAG = "ImageScanner";
    private int mIncreaseProgress;
    private Context mContext;

    public ImageScanner(Context context, TrashHandler handler) {
        super(context, handler);
        this.mContext = context;
    }

    @Override
    public void scan(final int increaseProgress) {

        if (!mIsScanning) {
            mIsScanning = true;
            doScan(increaseProgress);
            mIsScanning = false;
        }
    }

    private void doScan(final int increaseProgress) {
        mIncreaseProgress = increaseProgress;
        ContentResolver contentResolver = mContext.getContentResolver();
        MediaHelper.getHelper(contentResolver).getImagesBucketList(mStopRequested,
                imageScanCallBack);
    }

    ImageScanCallBack imageScanCallBack = new ImageScanCallBack() {

        @Override
        public void emptyCursor() {
            mTrashHandler.updateProgress(mIncreaseProgress, null);
            mTrashHandler.onTrashTypeFinish(TrashType.IMAGE_FILE, 0);
        }

        @Override
        public void updateProgress(int index, int count, String path) {
            int progressFile = calculateProgress(mIncreaseProgress, index, count);
            mTrashHandler.updateProgress(progressFile, path);
        }

        @Override
        public void endProgress(long totalSize) {
            mTrashHandler.onTrashTypeFinish(TrashType.IMAGE_FILE, totalSize);
        }

        @Override
        public void endAll(List<ImageTrashBucket> imageList) {
            for (ImageTrashBucket trash : imageList) {
                mTrashesData.putTrash(TrashType.IMAGE_FILE, trash);
                if (FeatureConfig.DEBUG_LOG) {
                    LogHelper.i(TAG, "[trash_image] filePath:" + trash.filePath + " size:"
                            + trash.size);
                }
            }
        }
    };

}
